package com.wgc.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgc.goods.domain.Product;
import com.wgc.goods.domain.ProductDetails;
import com.wgc.goods.mapper.ProductMapper;
import com.wgc.goods.service.ProductDetailsService;
import com.wgc.goods.mapper.ProductDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 15776
 * @description 针对表【goods_product_details(商品详情表)】的数据库操作Service实现
 * @createDate 2024-07-30 12:24:38
 */
@Service
public class ProductDetailsServiceImpl extends ServiceImpl<ProductDetailsMapper, ProductDetails>
        implements ProductDetailsService {
    @Autowired
    private ProductDetailsMapper iProductDetailsMapper;
    @Autowired
    private ProductMapper iProductMapper;

    @Override
    public List<ProductDetails> selectAll() {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetails::getIsDelete, 0);
        return iProductDetailsMapper.selectList(lqw);
    }


    @Override
    public List<ProductDetails> selectList(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = this.lqw(iProductDetails);
        return iProductDetailsMapper.selectList(lqw);
    }

    @Override
    public Integer insert(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetails::getProductId, iProductDetails.getProductId());
        lqw.eq(ProductDetails::getIsDelete, 0);
        lqw.orderByAsc(ProductDetails::getPrice);
        List<ProductDetails> list = iProductDetailsMapper.selectList(lqw);
        Product product = new Product();
        product.setId(iProductDetails.getId());
        if (list.size() > 0) {
            if (iProductDetails.getPrice().compareTo(list.get(0).getPrice()) > 0) {
                product.setPrice(list.get(0).getPrice());
            } else {
                product.setPrice(iProductDetails.getPrice());
            }
            iProductMapper.updateById(product);
        } else {
            product.setPrice(iProductDetails.getPrice());
            iProductMapper.updateById(product);
        }
        return iProductDetailsMapper.insert(iProductDetails);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                ProductDetails iProductDetails = new ProductDetails();
                iProductDetails.setId(Long.parseLong(id));
                iProductDetails.setIsDelete(1);
                iProductDetailsMapper.updateById(iProductDetails);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(ProductDetails iProductDetails) {
        return iProductDetailsMapper.updateById(iProductDetails);
    }

    @Override
    public Page<ProductDetails> pageAll(Integer page, Integer size) {
        Page<ProductDetails> page1 = new Page<>(page, size);
        return this.page(page1, null);
    }

    @Override
    public Page<ProductDetails> pageList(ProductDetails iProductDetails, Integer page, Integer size) {
        Page<ProductDetails> page1 = new Page<>(page, size);
        return this.page(page1, lqw(iProductDetails));
    }
    @Override
    public Integer reduceStock(Long productId, Integer num) {
        return iProductDetailsMapper.reduceStock(productId,num);
    }
    public LambdaQueryWrapper lqw(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        if (iProductDetails.getId() != null) {
            lqw.eq(ProductDetails::getId, iProductDetails.getId());
        }
        if (iProductDetails.getSpecId() != null) {
            lqw.eq(ProductDetails::getSpecId, iProductDetails.getSpecId());
        }
        if (iProductDetails.getNum() != null) {
            lqw.eq(ProductDetails::getNum, iProductDetails.getNum());
        }
        if (iProductDetails.getWeight() != null) {
            lqw.eq(ProductDetails::getWeight, iProductDetails.getWeight());
        }
        if (iProductDetails.getPrice() != null) {
            lqw.eq(ProductDetails::getPrice, iProductDetails.getPrice());
        }
        if (iProductDetails.getInventory() != null) {
            lqw.eq(ProductDetails::getInventory, iProductDetails.getInventory());
        }
        if (iProductDetails.getImg() != null) {
            lqw.eq(ProductDetails::getImg, iProductDetails.getImg());
        }
        if (iProductDetails.getProductId() != null) {
            lqw.eq(ProductDetails::getProductId, iProductDetails.getProductId());
        }
        if (iProductDetails.getIsDelete() != null) {
            lqw.eq(ProductDetails::getIsDelete, iProductDetails.getIsDelete());
        }
        if (iProductDetails.getCreateTime() != null) {
            lqw.eq(ProductDetails::getCreateTime, iProductDetails.getCreateTime());
        }
        if (iProductDetails.getUpdateTime() != null) {
            lqw.eq(ProductDetails::getUpdateTime, iProductDetails.getUpdateTime());
        }
        return lqw;
    }

}




